<?php

defined('SYSPATH') or die('No direct script access.');

class Model_Curl extends Model {

    public static $user_cookie_file;
    
    public static function get($url) {
        self::$user_cookie_file = $_SERVER['DOCUMENT_ROOT'] . '/cookies.txt';
        $cc = curl_init($url);
        curl_setopt($cc, CURLOPT_URL, $url);
        curl_setopt($cc, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($cc, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($cc, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cc, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16");
        curl_setopt($cc, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($cc, CURLOPT_COOKIEJAR, self::$user_cookie_file);
        curl_setopt($cc, CURLOPT_COOKIEFILE, self::$user_cookie_file);
        $html = curl_exec($cc);
        if ($html === false) {
            sleep(2);
            $html = curl_exec($cc);
        }
        curl_close($cc);
        return $html;
    }

    public static function post($url, $data) {
        self::$user_cookie_file = $_SERVER['DOCUMENT_ROOT'] . '/cookies.txt';
        $header = array('Host: oauth.yandex.ru','Content-type:application/x-www-form-urlencoded','Content-Length: '.strlen($data));
        $cc = curl_init($url);
        curl_setopt($cc, CURLOPT_URL, $url);
        curl_setopt($cc, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cc, CURLOPT_POST, true);
        curl_setopt($cc, CURLOPT_HTTPHEADER, $header);
        curl_setopt($cc, CURLOPT_POSTFIELDS, $data);
        curl_setopt($cc, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16");       
        curl_setopt($cc, CURLOPT_COOKIEJAR, self::$user_cookie_file);
        curl_setopt($cc, CURLOPT_COOKIEFILE, self::$user_cookie_file);
        $html = curl_exec($cc);
        curl_close($cc);
        return $html;
    }

}